package com.kgc.crowd.distributioncrowdwebui.controller;

import com.kgc.crowd.distributioncrowdcommon.VO.MemberSignSuccessVO;
import com.kgc.crowd.distributioncrowdcommon.VO.MemberVO;
import com.kgc.crowd.distributioncrowdcommon.api.MemberMangerRemoteService;
import com.kgc.crowd.distributioncrowdcommon.api.ProjectMangerRemoteService;
import com.kgc.crowd.distributioncrowdcommon.pojo.Member;
import com.kgc.crowd.distributioncrowdcommon.pojo.ProjectPO;
import com.kgc.crowd.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crowd.distributioncrowdcommon.pojo.ReturnPO;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowdcommon.util.EmailUtils;
import com.kgc.crowd.distributioncrowdwebui.config.IdentificationConfig;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/member/manager")
@Controller
public class MemberController {
    @Resource
    MemberMangerRemoteService memberMangerRemoteService;
    @Resource
    ProjectMangerRemoteService projectMangerRemoteService;
    @RequestMapping("/do/login")
    public String login(MemberVO memberVO, HttpSession session, Model model){
        String loginacct = memberVO.getLoginacct();
        String userpswd = memberVO.getUserpswd();
        System.out.println(memberVO.toString());
        ResultEntity<MemberSignSuccessVO> loginResultEntity = memberMangerRemoteService.login(loginacct, userpswd);
        if(loginResultEntity.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_ACCESS_DENIED);
            return "login";
        }
        MemberSignSuccessVO loginResultEntityData = loginResultEntity.getData();
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,loginResultEntityData);

        return "member";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        MemberSignSuccessVO memberSignSuccessVO = (MemberSignSuccessVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        if(memberSignSuccessVO==null){
            return "redirect:/index";
        }
        String token = memberSignSuccessVO.getToken();
        ResultEntity<String> resultEntity = memberMangerRemoteService.logout(token);
        if(ResultEntity.FAILED.equals(resultEntity.getResult())){
                throw new RuntimeException(resultEntity.getMessage());
        }
        session.removeAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        return "redirect:/index";
    }
    @RequestMapping("/send/code")
    @ResponseBody
    public ResultEntity<String> send(String phoneNum){
        ResultEntity<String> resultEntity = memberMangerRemoteService.sendCode(phoneNum);
        return resultEntity;
    }
    @RequestMapping("/register")
    public String register(MemberVO memberVO,Model model){
        ResultEntity<String> register=memberMangerRemoteService.register(memberVO);
        if (register.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,register.getMessage());
            return "reg";
        }
        return "login";
    }

    @RequestMapping("/minecrowdfunding")
    public String tominecrowdfunding(Model model){
        ResultEntity<List<ProjectPO>> listResultEntity = projectMangerRemoteService.selectProject();
        ResultEntity<List<ReturnPO>> returnPo = projectMangerRemoteService.selectReturnPo();
        model.addAttribute("returnList",returnPo.getData());
        model.addAttribute("projectlist",listResultEntity.getData());
        return "minecrowdfunding";
    }
    @RequestMapping("/member")
    public String tomember(){
        return "member";
    }
    @RequestMapping("/to/accttype")
    public String toaccttype(){
        return "accttype";
    }
    @RequestMapping("/to/apply")
    public String toapply(@RequestParam("accttype")  Byte accttype,HttpSession session){
        MemberSignSuccessVO memberSignSuccessVO = (MemberSignSuccessVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        ResultEntity<Member> memberResultEntity = memberMangerRemoteService.retrieveMemberByLoginAcct(memberSignSuccessVO.getUsername());
        Member member=memberResultEntity.getData();
        member.setAccttype(accttype);
        member.setAuthstatus(Byte.parseByte("1"));
        System.out.println(member.getLoginacct());
        System.out.println(member.getUserpswd());
        session.setAttribute("memberAccount",member.getLoginacct());
        session.setAttribute("memberPassword",member.getUserpswd());
        session.setAttribute("memberId",member.getId());
        session.setAttribute("memberAccttype",member.getAccttype());
        session.setAttribute("memberAuthstatus",member.getAuthstatus());
        return "apply";
    }
    @RequestMapping("/identification")
    @ResponseBody
    public ResultEntity<String> identification(String idNo,String name){
        String identification = IdentificationConfig.identification(idNo, name);
        return ResultEntity.successWithData(identification);
    }
    @RequestMapping("/to/apply-1")
    public String toapply1(){
        return "apply-1";
    }
    @RequestMapping("/to/apply-2")
    public String toapply2(){
        return "apply-2";
    }
    @RequestMapping("/to/apply-3")
    public String toapply3(@RequestParam("email") String email,HttpSession session){
        String code = CrowdUtils.randomCode(6);
        EmailUtils.sendemail(email,code);
        session.setAttribute("code",code);
        return "apply-3";
    }
    @RequestMapping("/code/complete")
    public String codecomplete(@RequestParam("verCode") String verCode,HttpSession session,Model model){
        String  code = (String)session.getAttribute("code");
        if(code.equals(verCode)){
            Integer memberId = (Integer)session.getAttribute("memberId");
            Byte memberAccttype = (Byte)session.getAttribute("memberAccttype");
            Byte memberAuthstatus = (Byte)session.getAttribute("memberAuthstatus");
            String account = (String)session.getAttribute("memberAccount");
            String memberPassword = (String)session.getAttribute("memberPassword");
            Member member=new Member();
            member.setId(memberId);
            member.setAccttype(memberAccttype);
            member.setAuthstatus(memberAuthstatus);
            ResultEntity<Integer> integerResultEntity = memberMangerRemoteService.updateMember(member);
            if(integerResultEntity.getData()>0){
                //退出登录
                MemberSignSuccessVO memberSignSuccessVO = (MemberSignSuccessVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
                if(memberSignSuccessVO==null){
                    return "redirect:/index";
                }
                String token = memberSignSuccessVO.getToken();
                ResultEntity<String> resultEntity = memberMangerRemoteService.logout(token);
                if(ResultEntity.FAILED.equals(resultEntity.getResult())){
                    throw new RuntimeException(resultEntity.getMessage());
                }
                session.removeAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
                //重新登录
                ResultEntity<MemberSignSuccessVO> login = memberMangerRemoteService.login(account, memberPassword);
                if(login.getResult().equals(ResultEntity.FAILED)){
                    model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_ACCESS_DENIED);
                    return "login";
                }
                MemberSignSuccessVO loginResultEntityData = login.getData();
                session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,loginResultEntityData);
                return "member";
            }
            model.addAttribute("msg","实名认证失败");
            return "apply-3";
        }
        model.addAttribute("msg","验证码错误");
        return "apply-3";
    }
}
